package com.kotlin.sample.controller

import com.kotlin.sample.bean.Message
import com.kotlin.sample.service.MessageService
import org.springframework.web.bind.annotation.*

@RestController
class MessageController(val service: MessageService) {

    /**
     * http://localhost:8080/?name=1
     */
    @GetMapping("/")
    fun index(@RequestParam("name") name: String) = "Hello, $name!"

    /**
     * http://localhost:8080/
     */
    @GetMapping("/{id}")
    fun getById(@PathVariable id: String): List<Message> =
        service.findMessageById(id)

    /**
     * http://localhost:8080/list
     */
    @GetMapping("/list")
    fun list() = listOf(
        Message("1", "Hello!"),
        Message("2", "Bonjour!"),
        Message("3", "Privet!"),
    )


    /**
     * http://localhost:8080/list-all
     */
    @GetMapping("/list-all")
    fun listAll(): List<Message> = service.findMessages()

    /**
     * http://localhost:8080/save
     */
    @PostMapping("/save")
    fun save(@RequestBody message: Message) {
        service.save(message)
    }

}